﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VTFC
{
    // ----------------------------------------------------------------------------
    /// <summary>
    /// Content.tfc XML schema class.
    /// </summary>
    /// <remarks>
    /// Contains information and metadata about TFS documents.
    /// </remarks>
    // ----------------------------------------------------------------------------
    [Serializable]
    public class Content
    {
        /// <summary>TFS server url.</summary>
        public string ServerUrl { get; set; }
        /// <summary>DateTime of the get latest version.</summary>
        public DateTime GetLatestVersion { get; set; }
        /// <summary>Document list.</summary>
        public List<Document> Documents { get; set; }
        /// <summary>Document library list.</summary>
        public List<List> Lists { get; set; }
        /// <summary>Document icon list.</summary>
        public List<Icon> Icons { get; set; }

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        // ----------------------------------------------------------------------------
        public Content()
        {
        }//ctor

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Search a <see cref="VTFC.Document"/> in Files.
        /// </summary>
        /// <param name="url">Full url of the file.</param>
        /// <returns><see cref="VTFC.Document"/> instance if it exists.</returns>
        // ----------------------------------------------------------------------------
        public Document GetDocumentByUrl(string url)
        {
            return Documents.Find(d => d.Url.Equals(url));
        }//func GetDocumentByUrl

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Search a <see cref="VTFC.List"/> in Lists.
        /// </summary>
        /// <param name="name">Name of the list.</param>
        /// <returns><see cref="VTFC.List"/> instance if it exists.</returns>
        // ----------------------------------------------------------------------------
        public List GetListByName(string name)
        {
            return Lists.Find(l => l.Name.Equals(name));
        }//func GetListByName

        // ----------------------------------------------------------------------------
        /// <summary>
        /// All <see cref="VTFC.Document"/> in the folder or list.
        /// </summary>
        /// <param name="folderpath">Path of the folder.</param>
        /// <returns><see cref="VTFC.Document"/> in a list.</returns>
        // ----------------------------------------------------------------------------
        public List<Document> GetFilesByDirectoryPath(string folderpath)
        {
            return Documents.FindAll((d => d.MetaData.Directory.Equals(folderpath)));
        }//func GetFilesByDirectoryPath

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Get index of the extension in the Icons list.
        /// </summary>
        /// <remarks>
        /// By default, unknown file icon is with index 0, therfore an offset value <br/> 
        /// must be added.
        /// </remarks>
        /// <param name="extension">Extension of the file.</param>
        /// <returns>Index of the icon.</returns>
        // ----------------------------------------------------------------------------
        public int GetIconIndex(string extension)
        {
            Icon icon = Icons.Find(i => i.Extension.Equals(extension));
            return icon == null ? 1 : Icons.IndexOf(icon) + Constant.VTFC_IMGLISTFILE_OFFSET;
        }//func GetIconIndex

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Order the Lists, Folders and Files.
        /// </summary>
        // ----------------------------------------------------------------------------
        public void Order()
        {
            if (Lists != null)
            {
                Lists = Lists.OrderBy(l => l.Name).ToList();

                foreach (List list in Lists)
                {
                    if (list.Folders != null)
                    {
                        list.Folders = list.Folders.OrderBy(f => f.Directory).ToList();
                    }
                }
            }
            if (Documents != null)
            {
                Documents = Documents.OrderBy(d => d.Url).ToList();
            }
        }//func Order
    }//class Content    
}//namespace VTFC
